package com.hulk.boot.config;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.google.common.base.Throwables;

import lombok.extern.slf4j.Slf4j;

/** 
* @author 作者  cmt
* 类说明 
*/

@Configuration
@EnableAsync
@Slf4j
public class AsyncConfig implements AsyncConfigurer {

	@Override
	public Executor getAsyncExecutor() {
		 ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
         executor.setCorePoolSize(7);
         executor.setMaxPoolSize(42);
         executor.setQueueCapacity(11);
         executor.setThreadNamePrefix("MyExecutor-");
         executor.initialize();
         return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		 return new MyAsyncUncaughtExceptionHandler();
	}
	
	
	
	private class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {  
	  
	    @Override  
	    public void handleUncaughtException(Throwable throwable, Method method, Object... args) { 
	    	  String argArrays =  Arrays.toString(args);
	    	  log.error("Unexpected error occurred invoking async.............. method:{},args:{},Throwable:{}",
						new Object[] { method.toGenericString(),argArrays, Throwables.getStackTraceAsString(throwable)  });
	    }  
	} 

}
